﻿using System;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace Bpms.Control
{
    /// <summary>
    /// 分页控件
    /// </summary>
    [ToolboxData("<{0}:Pager runat=\"server\"> </{0}:Pager>")]
    public class Pager : BaseControl
    {
        int curPage = 1;
        int countPage = 1;
        string url = null;
        int extendPage = 5;

        public Pager()
        {

        }

        public Pager(int curPage, int countPage, string url, int extendPage)
        {
            this.curPage = curPage;
            this.countPage = countPage;
            this.url = url;
            this.extendPage = extendPage;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.WriteLine(GetPageNumbers(this.curPage, this.countPage, this.url, extendPage));
            writer.WriteLine("<script>function PageRange(pageID){if(!new RegExp(\"^[1-9]\\d*\\.?[0]*$\",\"g\").test(pageID)) return 1; else if (pageID<1) return 1; else if(pageID>" + countPage + ") return " + countPage + ";else return pageID;};function clickGoButton(e){var key ;if(window.event){key=e.keyCode;}else if(e.which){key=e.which;};if(key==13){document.getElementById('btnGoto_oxcc').focus();} }</script>");
           
            base.Render(writer);
        }

        public string GetPageNumbers(int curPage, int countPage, string url, int extendPage)
        {
            int startPage = 1;
            int endPage = 1;
            int indexof = url.IndexOf('?');


            if (indexof != -1)
            {
                int rightStringLength = url.Substring(indexof).Trim().Length;

                if (rightStringLength > 1)
                {
                    url = url + "&";
                }
            }
            else
            {
                url = url + "?";
            }

            string t1 = "<a href=\"" + url + "page=1" + "\">首页</a>&nbsp;";
            string t2 = "&nbsp;<a href=\"" + url + "page=" + countPage + "\">尾页</a>&nbsp;";

            if (countPage < 1) countPage = 1;
            if (extendPage < 3) extendPage = 2;

            if (countPage > extendPage)
            {
                if (curPage - (extendPage / 2) > 0)
                {
                    if (curPage + (extendPage / 2) < countPage)
                    {
                        startPage = curPage - (extendPage / 2);
                        endPage = startPage + extendPage - 1;
                    }
                    else
                    {
                        endPage = countPage;
                        startPage = endPage - extendPage + 1;
                        t2 = "";
                    }
                }
                else
                {
                    endPage = extendPage;
                    t1 = "";
                }
            }
            else
            {
                startPage = 1;
                endPage = countPage;
                t1 = "";
                t2 = "";
            }

            StringBuilder s = new StringBuilder("");

            s.Append(t1);
            for (int i = startPage; i <= endPage; i++)
            {
                if (i == curPage)
                {
                    s.Append("&nbsp;");
                    s.Append("[" + i + "]");
                    s.Append("&nbsp;");
                }
                else
                {
                    s.Append("&nbsp;<a href=\"");
                    s.Append(url);
                    s.Append("page=");
                    s.Append(i);
                    s.Append("\">");
                    s.Append("[" + i + "]");
                    s.Append("</a>&nbsp;");
                }
            }
            s.Append(t2);

            string t3 = "&nbsp;<input onkeydown=\"clickGoButton(event);\" type=\"text\" id=\"gopage\" size=\"1\" value=\"" + curPage + "\">&nbsp;<input id=\"btnGoto_oxcc\" type=\"button\" value=\"跳转\" onclick=\"javascript:window.location='" + url + "page='+PageRange(document.getElementById('gopage').value);\" style=\"cursor:pointer;\">";
            s.Append(t3);

            return s.ToString();
        }

        /// <summary>
        /// 当前页
        /// </summary>
        [Description("当前页"), DefaultValue(1)]
        public int CurPage
        {
            set
            {
                curPage = value;
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        [Description("总页数"), DefaultValue(1)]
        public int CountPage
        {
            set
            {
                countPage = value;
            }
        }

        /// <summary>
        /// 路径
        /// </summary>
        [Description("地址"), DefaultValue("")]
        public string Url
        {
            set
            {
                url = value;
            }
        }

        /// <summary>
        /// 显示页码数
        /// </summary>
        [Description("显示页码数"), DefaultValue(5)]
        public int ExtendPage
        {
            set
            {
                extendPage = value;
            }
        }

    }
}
